//
//  AchievementLayer.cpp
//  ATM Killer
//
//  Created by USoft on 7/24/14.
//
//

#include "AchievementLayer.h"
AchievementLayer::AchievementLayer(){
    this->gameModel = GameModel::getInstance();
    this->achievementEffectType = -1;
    this->currentChoosenPage = 0;
    this->cardChoice = -1;
    this->currentAchievementId = 0;
    this->initialize();
    
}

AchievementLayer::~AchievementLayer(){
    Utils::safeRelease(this->pageButtons);
}

void AchievementLayer::onEnter() {
    Layer::onEnter();
    touchesRegister();
}

void AchievementLayer::onExit() {
    Layer::onExit();
    this->removeAllChildrenWithCleanup(true);
}

void AchievementLayer::onTouchesBegan(const std::vector<Touch*>& touches, Event* event) {
    if (this->gameModel->getGameState() == GS_ACHIEVEMENT && this->cardDirection == NUM_CARDS_HALT && this->isPopUpShow == false) {
        for(auto touch : touches){
            Point point = this->convertTouchToNodeSpace(touch);
            Point boardPoint = point - Point(this->achievementBoard->getPositionX() - this->achievementBoard->getContentSize().width/2, this->achievementBoard->getPositionY() - this->achievementBoard->getContentSize().height/2);
            //check page buttons
            //            for(int i = 0; i < this->pageButtons->count(); i++) {
            //                Face* pageButtonFace = (Face*) this->pageButtons->getObjectAtIndex(i);
            //                RectBody* pageButtonRect = new RectBody(pageButtonFace->getContentSize().width, pageButtonFace->getContentSize().height);
            //                pageButtonRect->setPosition(Point(pageButtonFace->getPositionX(), pageButtonFace->getPositionY()));
            //                if (Utils::checkPointInRect(pageButtonRect, boardPoint)) {
            //                    this->currentChoosenPage = i;
            //                }
            //            }
            
            //check cards
            for(int i = 0; i < this->gameModel->getAchievements()->count(); i++) {
                AbstractAchievement* achievement = (AbstractAchievement*)gameModel->getAchievements()->getObjectAtIndex(i);
                Face* itemFace = achievement->getFace();
                if(this->boardContent->isNodeVisible(itemFace)) {
                    RectBody* itemRect = new RectBody(itemFace->getContentSize().width, itemFace->getContentSize().height);
                    itemRect->setPosition(Point(itemFace->getPositionX() + boardContent->getPositionX(), itemFace->getPositionY() + boardContent->getPositionY()) + this->boardContent->getContentOffset());
                    if (Utils::checkPointInRect(itemRect, boardPoint) && itemFace->isVisible() == true) {
                        this->cardChoice = i;
                    }
                }
            }
            //check swipe
            RectBody* cardsRegion = new RectBody(800, 310);
            cardsRegion->setPosition(Point(this->achievementBoard->getContentSize().width/2,220));
            if (Utils::checkPointInRect(cardsRegion, boardPoint)) {
                this->swipe = true;
                this->timeOfSwipe = 0.0f;
                this->pointSwipe = point;
            }
        }
    }
}

void AchievementLayer::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
{
    if (this->gameModel->getGameState() == GS_ACHIEVEMENT && this->cardDirection == NUM_CARDS_HALT && this->isPopUpShow == false) {
        for(auto touch : touches){
            Point point = this->convertTouchToNodeSpace(touch);
            float distance = Utils::distance(point, this->pointSwipe);
            if(distance > 50) {
                this->cardChoice = -1;
            }
        }
    }
}

void AchievementLayer::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
    if (this->gameModel->getGameState() == GS_ACHIEVEMENT && this->cardDirection == NUM_CARDS_HALT && this->isPopUpShow == false) {
        for(auto touch : touches) {
            Point point = this->convertTouchToNodeSpace(touch);
            Point boardPoint = point - Point(this->achievementBoard->getPositionX() - this->achievementBoard->getContentSize().width/2, this->achievementBoard->getPositionY() - this->achievementBoard->getContentSize().height/2);
            //card choice
            for(int i = 0; i < this->gameModel->getAchievements()->count(); i++) {
                AbstractAchievement* achievement = (AbstractAchievement*)gameModel->getAchievements()->getObjectAtIndex(i);
                Face* itemFace = achievement->getFace();
                RectBody* itemRect = new RectBody(itemFace->getContentSize().width, itemFace->getContentSize().height);
                itemRect->setPosition(Point(itemFace->getPositionX() + boardContent->getPositionX(), itemFace->getPositionY() + boardContent->getPositionY()) + this->boardContent->getContentOffset());
                if (Utils::checkPointInRect(itemRect, boardPoint) && itemFace->isVisible() == true) {
                    if (this->cardChoice == i) {
                        this->currentAchievementId = i;
                    }
                }
            }
            //check swipe
            RectBody* cardsRegion = new RectBody(800, 310);
            cardsRegion->setPosition(Point(this->achievementBoard->getContentSize().width/2,220));
            float distance = Utils::distance(point, this->pointSwipe);
            float speed = distance / this->timeOfSwipe;
            if (this->swipe == true ) {
                if (speed > 150 && distance > 70) { // change page
                    if (point.x > this->pointSwipe.x){
                        if (this->currentChoosenPage > 0) {
                            this->currentChoosenPage -- ;
                            this->finishContentOffset = this->currentChoosenPage * (-772);
                            this->contentOffsetSpeed = 193;
                            this->cardDirection = NUM_CARDS_RIGHT;
                        }
                    }
                    if (point.x < this->pointSwipe.x){
                        if (this->currentChoosenPage < this->pageButtons->count() - 1) {
                            this->currentChoosenPage ++;
                            this->finishContentOffset = this->currentChoosenPage * (-772);
                            this->contentOffsetSpeed = -193;
                            this->cardDirection = NUM_CARDS_LEFT;
                        }
                    }
                }
            }
        }
        this->swipe = false;
        this->cardChoice = -1;
    }
}

void AchievementLayer::touchesRegister() {
    auto listener = EventListenerTouchAllAtOnce::create();
    listener->onTouchesBegan = CC_CALLBACK_2(AchievementLayer::onTouchesBegan, this);
    listener->onTouchesMoved = CC_CALLBACK_2(AchievementLayer::onTouchesMoved, this);
    listener->onTouchesEnded = CC_CALLBACK_2(AchievementLayer::onTouchesEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}

void AchievementLayer::update(float dt) {
    if(gameModel->getGameState() == GS_ACHIEVEMENT) {
        this->achievementBoard->setVisible(true);
        this->grayLayer->setVisible(true);
    }
    else {
        this->achievementBoard->setVisible(false);
        this->grayLayer->setVisible(false);
    }
    //contentOffsetCards
    if (this->cardDirection == NUM_CARDS_RIGHT)
    {
        if (this->boardContent->getContentOffset().x <= this->finishContentOffset - 1) {
            this->boardContent->setContentOffset(this->boardContent->getContentOffset() + Point(contentOffsetSpeed, 0));
        } else {
            this->boardContent->setContentOffset(Point(this->finishContentOffset,0));
            this->cardDirection = NUM_CARDS_HALT;
        }
        
    } else if (this->cardDirection == NUM_CARDS_LEFT) {
        if (this->boardContent->getContentOffset().x >= this->finishContentOffset + 1) {
            this->boardContent->setContentOffset(this->boardContent->getContentOffset() + Point(contentOffsetSpeed, 0));
        } else {
            this->boardContent->setContentOffset(Point(this->finishContentOffset,0));
            this->cardDirection = NUM_CARDS_HALT;
        }
    }
    //update information
    {
        AbstractAchievement* achievement = (AbstractAchievement*)gameModel->getAchievements()->getObjectAtIndex(this->currentAchievementId);
        //achievement type
        {
            char title[100];
            sprintf(title, "Achievement - %.0ld%%", this->gameModel->numOfUnlockedAchi * 100 / this->gameModel->getAchievements()->count());
            this->achievementTitle->setString(title);
        }
        //current money
        {
            char money[100];
            char* moneyText = gameModel->getScore()->display();
            sprintf(money,"$ %s", moneyText);
            delete[] moneyText;
            this->currentMoneyLabel->setString(money);
            this->currentMoneyLabel->setPosition(143, 26);
        }
        //current name
        {
            char achievementName[100];
            if (achievement->getStatus() == NUM_STATUS_LOCKED){
                sprintf(achievementName,"%s", "Locked Achievement");
            } else {
                sprintf(achievementName,"%s", achievement->getName().c_str());
            }
            this->currentNameLabel->setString(achievementName);
        }
        //current desc
        {
            char description[100];
            if (achievement->getStatus() == NUM_STATUS_LOCKED){
                sprintf(description, "%s", "\?\?\?-\?\?\?");
            } else {
                sprintf(description, "Description: %s", achievement->getDescription().c_str());
            }
            this->currentDescLabel->setString(description);
        }
        //current effect
        {
            char effect[100];
            if (achievement->getEffectType() == ACHIEVEMENT_EFFECT_DPS){
                sprintf(effect, "Effect: Increase %.2f %% DPS of all characters", achievement->getEffectPercent() * 100);
            } else if (achievement->getEffectType() == ACHIEVEMENT_EFFECT_GRANDMA){
                sprintf(effect, "Effect: Increase %.2f %% DPS of Grandma", achievement->getEffectPercent() * 100);
            } else if (achievement->getEffectType() == ACHIEVEMENT_EFFECT_GAMEDEV){
                sprintf(effect, "Effect: Increase %.2f %% DPS of Game developer", achievement->getEffectPercent() * 100);
            }
            this->currentEffectLabel->setString(effect);
        }
        //show button & show cost
        {
            if (achievement->getStatus() == NUM_STATUS_LOCKED) {
                this->showButton->setVisible(true);
                this->showCostLabel->setVisible(true);
            } else {
                this->showButton->setVisible(false);
                this->showCostLabel->setVisible(false);
            }
        }
    }
    //update achievements base on page
    {
        for (int i = 0; i < gameModel->getAchievements()->count(); i++) {
            AbstractAchievement* achievement = (AbstractAchievement*)gameModel->getAchievements()->getObjectAtIndex(i);
            Face* itemFace = achievement->getFace();
            Face* avatar = (Face*)itemFace->getChildByTag(0);
            //update selection
            if ( i == this->currentAchievementId){
                if (achievement->getStatus() == NUM_STATUS_UNLOCKED) {
                    itemFace->setSpriteFrame(ANI_ACHIEVEMENT_UNLOCK_2);
                    avatar->setVisible(true);
                    avatar->setPosition(itemFace->getContentSize().width/2, itemFace->getContentSize().height/2);
                }
                if (achievement->getStatus() == NUM_STATUS_LOCKED) {
                    itemFace->setSpriteFrame(ANI_ACHIEVEMENT_LOCK_2);
                    avatar->setVisible(false);
                    avatar->setPosition(itemFace->getContentSize().width/2, itemFace->getContentSize().height/2);
                }
                if (achievement->getStatus() == NUM_STATUS_BOUGHT) {
                    itemFace->setSpriteFrame(ANI_ACHIEVEMENT_BOUGHT_2);
                    avatar->setVisible(false);
                    avatar->setPosition(itemFace->getContentSize().width/2, itemFace->getContentSize().height/2);
                }
            } else {
                if (achievement->getStatus() == NUM_STATUS_UNLOCKED) {
                    itemFace->setSpriteFrame(ANI_ACHIEVEMENT_UNLOCK_1);
                    avatar->setVisible(true);
                    avatar->setPosition(itemFace->getContentSize().width/2, itemFace->getContentSize().height/2);
                }
                if (achievement->getStatus() == NUM_STATUS_LOCKED) {
                    itemFace->setSpriteFrame(ANI_ACHIEVEMENT_LOCK_1);
                    avatar->setVisible(false);
                    avatar->setPosition(itemFace->getContentSize().width/2, itemFace->getContentSize().height/2);
                }
                if (achievement->getStatus() == NUM_STATUS_BOUGHT) {
                    itemFace->setSpriteFrame(ANI_ACHIEVEMENT_BOUGHT_1);
                    avatar->setVisible(false);
                    avatar->setPosition(itemFace->getContentSize().width/2, itemFace->getContentSize().height/2);
                }
                
            }
        }
    }
    //update current Achievement avatar
    {
        AbstractAchievement* achievement = (AbstractAchievement*)gameModel->getAchievements()->getObjectAtIndex(this->currentAchievementId);
        Face* avatar = (Face*) this->currentAchievementFace->getChildByTag(0);
        if (achievement->getStatus() == NUM_STATUS_UNLOCKED) {
            char nameOfAvatar[100];
            sprintf(nameOfAvatar,"achievement/%d.png", this->currentAchievementId);
            this->currentAchievementFace->setSpriteFrame(ANI_ACHIEVEMENT_UNLOCK_1);
            avatar->setVisible(true);
            avatar->setSpriteFrame(nameOfAvatar);
        }
        if (achievement->getStatus() == NUM_STATUS_LOCKED) {
            this->currentAchievementFace->setSpriteFrame(ANI_ACHIEVEMENT_LOCK_1);
            avatar->setVisible(false);
        }
        if (achievement->getStatus() == NUM_STATUS_BOUGHT) {
            this->currentAchievementFace->setSpriteFrame(ANI_ACHIEVEMENT_BOUGHT_1);
            avatar->setVisible(false);
        }
        
    }
    //update page buttons
    {
        for (int i = 0; i < this->pageButtons->count(); i++) {
            Face* button = (Face*)this->pageButtons->getObjectAtIndex(i);
            if ( i == this->currentChoosenPage) {
                button->setSpriteFrame(ANI_PAGE_CHOSE_1);
            } else {
                button->setSpriteFrame(ANI_PAGE_CHOSE_2);
            }
        }
    }
    
}

void AchievementLayer::initialize() {
    this->pageButtons = __Array::create();
    CC_SAFE_RETAIN(this->pageButtons);
    this->boardLayer = 0;
    this->isPopUpShow = false;
    this->createLayers();
    this->createLayersContent();
    this->createPageButtons();
}

void AchievementLayer::createLayers(){
    this->grayLayer = UGLayerColor::create();
    this->grayLayer->initWithColor(Color4B(0,0,0,100));
    this->grayLayer->setVisible(false);
    this->addChild(this->grayLayer);
    
    this->boardLayer = UGLayer::create();
    this->addChild(this->boardLayer);
    
    this->popUpLayer = UGLayer::create();
    this->addChild(this->popUpLayer);
    
    this->cardsLayer = UGLayer::create();
}

void AchievementLayer::createLayersContent(){
    Size gameSize = gameModel->getDisplayResolutionSize();
    //board layer's content
    this->achievementBoard = new Face();
    this->achievementBoard->setPosition(gameSize.width/2, gameSize.height/2);
    this->achievementBoard->initWithSpriteFrameName(ANI_BOARD_GUI);
    
    //close button
    UGMenu* closeMenu = UGMenu::create();
    closeMenu->setPosition(Point::ZERO);
    this->achievementBoard->addChild(closeMenu);
    
    MenuItemSprite* closeButton = Utils::createButton((char*)ANI_BUTTON_CLOSE_1, (char*)ANI_BUTTON_CLOSE_2);
    closeButton->setPosition(this->achievementBoard->getContentSize().width + closeButton->getContentSize().width/2 - 20, this->achievementBoard->getContentSize().height - closeButton->getContentSize().height/2);
    closeMenu->addChild(closeButton);
    closeButton->setCallback(CC_CALLBACK_1(AchievementLayer::onCloseAchievementBoard, this));
    //achievementTitle
    this->achievementTitle = Utils::createLabel((char*)"Achievement",(char*)FONT_TRANSFORMER, 32);
    this->achievementTitle->setPosition(this->achievementBoard->getContentSize().width/2 - 150,this->achievementBoard->getContentSize().height - 35);
    this->achievementTitle->setDimensions(500, this->achievementTitle->getContentSize().height);
    
    this->achievementBoard->addChild(this->achievementTitle);
    this->achievementBoard->setVisible(false);
    this->boardLayer->addChild(this->achievementBoard);
    //money bar
    Face* moneyBar = new Face();
    moneyBar->initWithSpriteFrameName(ANI_MONEY_BAR);
    moneyBar->setPosition(this->achievementBoard->getContentSize().width - moneyBar->getContentSize().width/2 - 25, this->achievementBoard->getContentSize().height - 30);
    this->achievementBoard->addChild(moneyBar);
    
    this->currentMoneyLabel = Utils::createLabel((char*)"0", (char*)FONT_TRANSFORMER, 28);
    this->currentMoneyLabel->setDimensions(300, this->currentMoneyLabel->getDimensions().height);
    this->currentMoneyLabel->setPosition(moneyBar->getContentSize().width/2 - 30, moneyBar->getContentSize().height/2 - 3);
    moneyBar->addChild(this->currentMoneyLabel);
    //currentAchievement face
    this->currentAchievementFace = new Face();
    this->currentAchievementFace->initWithSpriteFrameName(ANI_ACHIEVEMENT_UNLOCK_1);
    this->currentAchievementFace->setPosition(95,500);
    this->achievementBoard->addChild(this->currentAchievementFace);
    
    char nameOfAvatar[100];
    sprintf(nameOfAvatar,"achievement/%d.png", this->currentAchievementId);
    Face* avatar = new Face();
    avatar->initWithSpriteFrameName(nameOfAvatar);
    avatar->setPosition(this->currentAchievementFace->getContentSize().width/2, this->currentAchievementFace->getContentSize().height/2);
    this->currentAchievementFace->addChild(avatar);
    avatar->setTag(0);
    
    
    
    //current name
    this->currentNameLabel = Utils::createLabel((char*)"The very first coin", (char*)FONT_TRANSFORMER, 32);
    this->currentNameLabel->setAnchorPoint(Point(0,0.5));
    this->currentNameLabel->setHorizontalAlignment(TextHAlignment::LEFT);
    this->currentNameLabel->setPosition(Point(160,535));
    this->currentNameLabel->setDimensions(400, this->currentNameLabel->getContentSize().height);
    this->achievementBoard->addChild(this->currentNameLabel);
    
    //current desc
    this->currentDescLabel = Utils::createLabel((char*)"Earn 1 coin from the machine", (char*)FONT_MYRIADPRO_BOLD, 20);
    this->currentDescLabel->setAnchorPoint(Point(0,0.5));
    this->currentDescLabel->setHorizontalAlignment(TextHAlignment::LEFT);
    this->currentDescLabel->setColor(Color3B(128,215,255));
    this->currentDescLabel->setPosition(Point(160,505));
    this->currentDescLabel->setDimensions(800, this->currentDescLabel->getContentSize().height);
    this->achievementBoard->addChild(this->currentDescLabel);
    
    //current effect
    this->currentEffectLabel = Utils::createLabel((char*)"Effect: Increase 10% DPS of all characters", (char*)FONT_MYRIADPRO_BOLD, 20);
    this->currentEffectLabel->setAnchorPoint(Point(0, 1));
    this->currentEffectLabel->setHorizontalAlignment(TextHAlignment::LEFT);
    this->currentEffectLabel->setColor(Color3B(128, 215, 255));
    this->currentEffectLabel->setPosition(Point(160, 490));
    this->currentEffectLabel->setDimensions(350, 200);
    this->achievementBoard->addChild(this->currentEffectLabel);
    //show button
    UGMenu* showMenu = UGMenu::create();
    showMenu->setPosition(Point::ZERO);
    this->achievementBoard->addChild(showMenu);
    this->showButton = Utils::createButton((char*)ANI_BUTTON_GREEN, (char*)ANI_BUTTON_GRAY, (char*)"show it now", 35, (char*)FONT_TRANSFORMER, Color3B(255,255,255));
    this->showButton->setPosition(655,480);
    showMenu->addChild(this->showButton);
    this->showButton->setCallback(CC_CALLBACK_1(AchievementLayer::showInfoPopUp, this));
    
    //show cost label
    this->showCostLabel = Utils::createLabel((char*)"$ 1 billion", (char*)FONT_TRANSFORMER, 28);
    this->showCostLabel->setAnchorPoint(Point(1,0));
    this->showCostLabel->setPosition(818,515);
    this->showCostLabel->setColor(Color3B(255,174,7));
    this->showCostLabel->setDimensions(400, this->showCostLabel->getContentSize().height);
    this->showCostLabel->setHorizontalAlignment(TextHAlignment::RIGHT);
    this->achievementBoard->addChild(this->showCostLabel);
    //content layer's content
    for(int i = 0; i < gameModel->getAchievements()->count(); i++)
    {
        int page = i / 21;
        int index = i % 21;
        int startWidth = 53 + 770 * page;
        int width = startWidth + (index % 7) * 110;
        int height = 325 - (110 * (int)(index / 7 ));
        AbstractAchievement* achievement = (AbstractAchievement*)gameModel->getAchievements()->getObjectAtIndex(i);
        Face* itemFace = achievement->getFace();
        itemFace->setPosition(width, height);
        this->cardsLayer->addChild(itemFace);
        
        char nameOfAvatar[100];
        sprintf(nameOfAvatar,"achievement/%d.png", achievement->getAchievementID());
        Face* avatar = new Face();
        avatar->initWithSpriteFrameName(nameOfAvatar);
        avatar->setPosition(itemFace->getContentSize().width/2 + 2, itemFace->getContentSize().height/2+2);
        itemFace->addChild(avatar);
        avatar->setTag(0);
        
    }
    this->cardsLayer->setContentSize(Size(2000,500));
    this->boardContent = ScrollView::create(Size(770, 400));
    this->boardContent->setPosition(Point(45,0));
    this->boardContent->setContainer(this->cardsLayer);
    this->boardContent->setDirection(ScrollView::Direction::HORIZONTAL);
    this->boardContent->setContentOffset(Point(0,0));
    this->boardContent->setTouchEnabled(false);
    this->achievementBoard->addChild(this->boardContent);
    
    //pop up
    this->loadPopUp();
}

void AchievementLayer::createPageButtons() {
    int numberOfPage = (int)this->gameModel->getAchievements()->count() / 21;
    if(this->gameModel->getAchievements()->count()%21 != 0) {
        numberOfPage += 1;
    }
    for(int i = 0; i < numberOfPage; i++) {
        Face* pageFace = new Face();
        pageFace->initWithSpriteFrameName(ANI_PAGE_CHOSE_2);
        pageFace->setPosition(425 - (numberOfPage-1) * 20 + i * 40, 40);
        this->achievementBoard->addChild(pageFace);
        this->pageButtons->addObject(pageFace);
    }
}

void AchievementLayer::loadPopUp() {
    Size gameSize = this->gameModel->getDisplayResolutionSize();
    this->popUpFace = new Face();
    this->popUpFace->initWithSpriteFrameName(ANI_POP_UP_BG);
    this->popUpFace->setPosition(gameSize.width/2, gameSize.height/2);
    this->popUpLayer->addChild(this->popUpFace);
    
    Label* labelPopUp1 = Utils::createLabel((char*)"Are you sure to spend", (char*)FONT_TRANSFORMER, 28);
    labelPopUp1->setColor(Color3B(255,255,255));
    labelPopUp1->setAnchorPoint(Point(0.5,1));
    labelPopUp1->setDimensions(350, 100);
    labelPopUp1->setPosition(this->popUpFace->getContentSize().width/2, 215);
    labelPopUp1->setTag(1);
    
    Label* labelPopUp2 = Utils::createLabel((char*)"$123.456 Million", (char*)FONT_TRANSFORMER,35);
    labelPopUp2->setColor(Color3B(255,174,7));
    labelPopUp2->setPosition(this->popUpFace->getContentSize().width/2, 150);
    labelPopUp2->setTag(2);
    
    Label* labelPopUp3 = Utils::createLabel((char*)"The content of this achievement will be revealed",(char*)FONT_MYRIADPRO_REGULAR, 17);
    labelPopUp3->setDimensions(labelPopUp1->getDimensions().width - 30, 200);
    labelPopUp3->setColor(Color3B(255,255,255));
    labelPopUp3->setAnchorPoint(Point(0.5,1));
    labelPopUp3->setPosition(this->popUpFace->getContentSize().width/2, 122);
    labelPopUp3->setTag(3);
    
    this->popUpFace->addChild(labelPopUp1);
    this->popUpFace->addChild(labelPopUp2);
    this->popUpFace->addChild(labelPopUp3);
    
    UGMenu* popUpMenu = UGMenu::create();
    popUpMenu->setPosition(Point::ZERO);
    // OK Button and Cancel button
    this->okButton = Utils::createButton((char*)ANI_BUTTON_OK_1, (char*)ANI_BUTTON_OK_2);
    this->okButton->setPosition(125,50);
    this->cancelButton = Utils::createButton((char*)ANI_BUTTON_CANCEL_1, (char*)ANI_BUTTON_CANCEL_2);
    cancelButton->setPosition(275,50);
    popUpMenu->addChild(this->okButton);
    popUpMenu->addChild(this->cancelButton);
    this->cancelButton->setCallback(CC_CALLBACK_1(AchievementLayer::cancelPopUp,this));
    this->popUpFace->addChild(popUpMenu);
    this->popUpFace->setVisible(false);
}

void AchievementLayer::cancelPopUp(Ref* sender) {
    this->isPopUpShow = false;
    this->popUpFace->setVisible(false);
}


void AchievementLayer::showInfoPopUp(Ref* sender) {
    BigNumber* aBillion = BigNumber::create();
    char* billionString = new char[10];
    sprintf(billionString, "%s", "1000000000");
    aBillion->initialize(billionString);
    delete[] billionString;
    if (this->gameModel->getScore()->compare(aBillion)>=0) {
        // set label
        char labelPopUp1Text[200];
        sprintf(labelPopUp1Text, TXT_SURE_LEVEL_UP);
        Label* labelPopUp1 = (Label*) this->popUpFace->getChildByTag(1);
        labelPopUp1->setString(labelPopUp1Text);
        labelPopUp1->setPosition(this->popUpFace->getContentSize().width/2, 205);
        
        char labelPopUp2Text[200];
        char* costCharacter = aBillion->display();
        sprintf(labelPopUp2Text,TXT_MONEY_FORMAT, costCharacter);
        delete[] costCharacter;
        Label* labelPopUp2 = (Label*)this->popUpFace->getChildByTag(2);
        labelPopUp2->setPosition(this->popUpFace->getContentSize().width/2, 150);
        labelPopUp2->setString(labelPopUp2Text);
        
        this->getCancelButton()->setVisible(true);
        this->getOkButton()->setPosition(125,50);
        this->availablePopUp = true;
        
    } else {
        char labelPopUp1Text[200];
        sprintf(labelPopUp1Text, TXT_NOT_ENOUGH_MONEY_ACHI);
        Label* labelPopUp1 = (Label*) this->popUpFace->getChildByTag(1);
        labelPopUp1->setString(labelPopUp1Text);
        labelPopUp1->setPosition(this->popUpFace->getContentSize().width/2, 215);
        
        char labelPopUp2Text[200];
        char* costAchievement = aBillion->display();
        sprintf(labelPopUp2Text,TXT_MONEY_FORMAT, costAchievement);
        delete[] costAchievement;
        Label* labelPopUp2 = (Label*)this->popUpFace->getChildByTag(2);
        labelPopUp2->setString(labelPopUp2Text);
        labelPopUp2->setPosition(this->popUpFace->getContentSize().width/2, 110);
        
        Label* labelPopUp3 = (Label*)this->popUpFace->getChildByTag(3);
        labelPopUp3->setVisible(false);
        this->getOkButton()->setPosition(200,50);
        this->getCancelButton()->setVisible(false);
        this->availablePopUp = false;
    }
    // set callback
    this->getOkButton()->setCallback(CC_CALLBACK_1(AchievementLayer::onButtonClick,this));
    this->getOkButton()->setTag(1);
    
    this->popUpFace->setVisible(true);
    this->isPopUpShow = true;
}

void AchievementLayer::onButtonClick(Ref* sender) {
    if (this->availablePopUp == true){
        if (sender == this->getOkButton()) {
            this->notify(REQ_REVEAL_ACHIVEMENT, EVT_CLICK_BUTTON, NULL, 0, __Integer::create(this->currentAchievementId));
            this->cancelPopUp(sender);
        }
    } else {
        this->cancelPopUp(sender);
    }
}

void AchievementLayer::onCloseAchievementBoard(Ref* sender) {
    if(this->isPopUpShow == false) {
        this->notify(REQ_PAUSE, EVT_CLICK_BUTTON, NULL, 0, NULL);
    }
}







